home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / PROG_TOO / C013.ZIP / RM.C < prev    next >
Text File  |  1990-01-19  |  4KB  |  160 lines

  1. /********************************************************************
  2.  * C Users Group (U.K) C Source Code Library File CUGLIB.013        *
  3.  * Inquiries to: M. Houston, 36 Whetstone Clo. Farquhar Rd.         *
  4.  * Edgbaston, Birmingham B15 2QN ENGLAND                *
  5.  ********************************************************************
  6.  * File name: rm.c
  7.  * Program name:rm 
  8.  * Source of file: Ron Wellstead
  9.  * Purpose: An MS-DOS copy of the UNIX utility of the same name.
  10.  * Changes: <who what when & why major changes have been made>      
  11.  ********************************************************************/
  12.  
  13. /*
  14.  *
  15.  *    @(#) rm.c 1.2 87/07/27 
  16.  *
  17.  *    UNIX style rm utility for dos
  18.  *
  19.  *    copyright (c) 1987 Ron Wellsted.
  20.  *    This software is provided on the understanding that it is
  21.  *    NOT to be used for commercial gain. It may be freely distributed
  22.  *    in source or object form among amateur and hobby computer users ONLY!
  23.  *
  24.  *    removes files and directories
  25.  *    usage:    rm [-rfi] files../directories
  26.  *    switches:    -r    recursive [USE WITH EXTREME CAUTION!]
  27.  *                will remove ALL sub-directories below
  28.  *                given names.
  29.  *            -i    inquire before removing file/directory
  30.  *            -f    prevents error message on non-existant
  31.  *                files, read-only files etc. (BUT WILL remove
  32.  *                r/o files).
  33.  *    written for Microsoft C, link with setargv.obj to expand wildcards
  34.  */
  35. #include    <stdio.h>
  36. #include    <process.h>
  37. #include    <sys/types.h>
  38. #include    <sys/stat.h>
  39. #include    <io.h>
  40.  
  41. char *name,*swch,buffer[256]="@(#)rm.c VR 1.0.0 15 Jul 1987";
  42. int fsw=0,rsw=0,isw=0;
  43. struct stat data;
  44.  
  45. main(argc,argv)
  46. int argc;
  47. char *argv[];
  48. {
  49.     int i,rm;
  50.  
  51.     if (argc>=2) swch=argv[1];
  52.     if ((argc<2)||(setsw(argv[1])<0))
  53.     {
  54.         fprintf(stderr,"usage rm [-fri] file1 [.... fileN]\n");
  55.         exit(1);
  56.     }
  57.     name=argv[0];
  58.     if (fsw|rsw|isw) i=2;
  59.     else i=1;
  60.     for (;i<argc;i++)
  61.     {
  62.         if (stat(argv[i],&data))
  63.         {
  64.             fprintf(stderr,"rm: ");
  65.             perror(argv[i]);
  66.             if (rsw) continue;
  67.             else exit(1);
  68.         }
  69.         if (data.st_mode & S_IFDIR)
  70.         {
  71.             if (rsw==0) continue;
  72.             if (cleardir(argv[i]))
  73.             {
  74.                 fprintf(stderr,"rm: cannot remove %s",argv[i]);
  75.                 if (rsw) continue;
  76.                 else exit(1);
  77.             }
  78.         }
  79.         else
  80.         {
  81.             if (!(data.st_mode & S_IWRITE))
  82.             {
  83.                 rm=fsw;
  84.                 if (!rm) rm=check(argv[i],"read only: ");
  85.                 if (rm) chmod(argv[i],S_IWRITE);
  86.             }
  87.             if (isw) rm=check(argv[i],"\0");
  88.             if (!rm) continue;
  89.             if (unlink(argv[i]))
  90.             {
  91.                 fprintf(stderr,"rm: ");
  92.                 perror(argv[i]);
  93.                 exit(1);
  94.             }
  95.         }
  96.     }
  97.     exit(0);
  98. }
  99.  
  100. setsw(ptr)
  101. char *ptr;
  102. {
  103.     char ch;
  104.     if (*ptr!='-') return 0;
  105.     for (ptr++;*ptr!='\0';ptr++)
  106.     {
  107.         ch=tolower(*ptr);
  108.         if (ch=='f') fsw=1;
  109.         else if (ch=='r') rsw=1;
  110.         else if (ch=='i') isw=1;
  111.         else
  112.         {
  113.             fprintf(stderr,"rm: %c: illegal switch n",ch);
  114.             return -1;
  115.         }
  116.     }
  117.     return 1;
  118. }
  119.  
  120. check(ptr,mode)
  121. char *ptr,*mode;
  122. {
  123.     char reply[20];
  124.  
  125.     printf("%s: %sremove ? ",ptr,mode);
  126.     if (gets(reply)==NULL) return 0;
  127.     if (tolower(reply[0])=='y') return 1;
  128.     else return 0;
  129. }
  130.  
  131. cleardir(dir)
  132. char *dir;
  133. {
  134.     char ch;
  135.     int retval;
  136.  
  137.     strcpy(buffer,dir);
  138.     ch=buffer[strlen(buffer)-1];
  139.     if ((ch!='\\')&&(ch!='/'))
  140.         strcat(buffer,"\\");
  141.     strcat(buffer,"*.*");
  142.     retval=spawnl(P_WAIT,name,name,swch,buffer,NULL);
  143.     if (retval<0)
  144.     {
  145.         perror("rm: ");
  146.         exit(1);
  147.     }
  148.     if (retval==0)
  149.     {
  150.         if (rmdir(dir))
  151.         {
  152.             fprintf(stderr,"rm: ");
  153.             perror(dir);
  154.             if (rsw) retval=1;
  155.             else exit(1);
  156.         }
  157.     }
  158.     return retval;
  159. }
  160.